﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Linq;

public partial class Admin_QuanLyHocVien : System.Web.UI.Page
{
    DataClassesDataContext data = new DataClassesDataContext();

    //HocVien hv ;
    //bool Them ;
    protected void Page_Load(object sender, EventArgs e)
    {
        //hv = new HocVien();
        //Btn_XoaSV.Attributes.Add("onClick", "javascript:return confirm('Bạn có chắc chắn muốn thực hiện thao tác này?');");
        
        //Them = false;
        
        if (!IsPostBack)
        {
            LoadDanhSach();
            //ViewState["Them"] = false;
        }
        //else
        //{
        //    //Them = (bool)ViewState["Them"];
        //    //Them = (bool)Request.Form.Get("Them".ToString());
        //}
    }

    //Lấy tất cả thông tin học viên
    private void LoadDanhSach()
    {
        //var query = from p in data.tbl_SINHVIENs select p;
        GridView_HocVien.DataSource = data.tbl_SINHVIENs;
        GridView_HocVien.DataBind();
    }

    //Thêm thuộc tính cho từng dòng GridView
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //e.Row.Attributes.Add("style", "background-color: #FFFFFF; color: black;");
            
            //e.Row.Attributes.Add("onmouseover", "style.backgroundColor='#FF6600'");
            //e.Row.Attributes.Add("onmouseout", "style.backgroundColor='#FFFFFF'");

            //e.Row.Attributes.Add("onmouseover", "this.setAttribute('bgColor', this.style.backgroundColor); this.style.backgroundColor = '#C2D69B';");
            e.Row.Attributes.Add("onmouseover", "this.setAttribute('bgColor', this.style.backgroundColor); this.style.backgroundColor = '#FF6600';");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor = this.getAttribute('bgColor');");
            //e.Row.Attributes.Add("onclick",this.GetPostBackClientEvent(GridView1,"Select$" + e.Row.RowIndex.ToString()));
            //e.Row.Attributes.Add("onclick", "GridView_selectRow(this,'" + e.Row.Cells[0].Text + "')");
            //e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(sender, "Select " + e.Row.RowIndex.ToString()));
            //e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
            //e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
            //e.Row.Attributes.Add("onclick", Page.ClientScript.GetPostBackEventReference(GridView1, "Select$" + e.Row.RowIndex.ToString(), true));
            tbl_SINHVIEN sv = e.Row.DataItem as tbl_SINHVIEN;
            (e.Row.FindControl("ImageButton2") as ImageButton).CommandArgument = sv.MsSinhVien.ToString();
            //string editIndex = e.Row.DataItemIndex.ToString();


            //ImageButton btn = e.Row.FindControl("btnEdit") as ImageButton;
            //if (btn != null) { btn.CommandArgument = editIndex; }
        }
    }
    
    
    #region
    public void GridView_selectRow(object sender, string a)
    {
        //GridView gv = (GridView)sender;
        //txtMSSV.Text = gv.SelectedRow.Cells[5].Text;
        //txtHoTenSV.Text = a;
        //txtLop.Text = a;
        //ddlGioiTinh.SelectedValue = a;
        //txtNgaySinh.Text = a;
        //txtDiaChi.Text = a;
        //txtEmail.Text = a;
        //txtSoDT.Text = a;
    }

    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        try
        {
            //TextBox1.Text = GridView1.SelectedRow.Cells[1].Text.ToString();
            //TextBox2.Text = GridView1.SelectedRow.Cells[2].Text.ToString();

            //GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
            //txtMSSV.Text = GridView1.SelectedRow.Cells[1].Text.ToString();
            //txtHoTenSV.Text = GridView1.SelectedRow.Cells[2].Text.ToString();
            //txtLop.Text = GridView1.SelectedRow.Cells[3].Text.ToString();
            //ddlGioiTinh.SelectedValue = GridView1.SelectedRow.Cells[4].Text.ToString();
            //txtNgaySinh.Text = GridView1.SelectedRow.Cells[5].Text.ToString();
            //txtDiaChi.Text = GridView1.SelectedRow.Cells[6].Text.ToString();
            //txtEmail.Text = GridView1.SelectedRow.Cells[7].Text.ToString();
            //txtSoDT.Text = GridView1.SelectedRow.Cells[8].Text.ToString();

        }
        catch
        {
        }
        finally
        {
        }
    }

    //Lấy thông tin cá nhân học viên
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //try
        //{
        //    txtMSSV.Text = GridView1.SelectedRow.Cells[0].Text.ToString();
        //    txtHoTenSV.Text = GridView1.SelectedRow.Cells[1].Text.ToString();
        //    txtLop.Text = GridView1.SelectedRow.Cells[2].Text.ToString();
        //    ddlGioiTinh.SelectedValue = GridView1.SelectedRow.Cells[3].Text.ToString();
        //    txtNgaySinh.Text = GridView1.SelectedRow.Cells[4].Text.ToString();
        //    txtDiaChi.Text = GridView1.SelectedRow.Cells[5].Text.ToString();
        //    txtEmail.Text = GridView1.SelectedRow.Cells[6].Text.ToString();
        //    txtSoDT.Text = GridView1.SelectedRow.Cells[7].Text.ToString();

        //}
        //catch
        //{
        //}
        //finally
        //{
        //}
    }
    private void EnableText()
    {
        //txtMSSV.Enabled = true;
        ////txtNgaySinh.Enabled = true;
        //txtLop.Enabled = true;
        //txtDiaChi.Enabled = true;
        //txtEmail.Enabled = true;
        //txtSoDT.Enabled = true;
        //txtHoTenSV.Enabled = true;
        //ddlGioiTinh.Enabled = true;
    }

    private void DisableText()
    {
        //txtMSSV.Enabled = false;
        //txtNgaySinh.Enabled = false;
        //txtLop.Enabled = false;
        //txtDiaChi.Enabled = false;
        //txtEmail.Enabled = false;
        //txtSoDT.Enabled = false;
        //txtHoTenSV.Enabled = false;
        //ddlGioiTinh.Enabled = false;
    }

    //private void ClearText()
    //{
    //    txtMSSV.Text = "";
    //    txtNgaySinh.Text = "";
    //    txtLop.Text = "";
    //    txtDiaChi.Text = "";
    //    txtEmail.Text = "";
    //    txtSoDT.Text = "";
    //    txtHoTenSV.Text = "";
    //}
    //Button Thêm
    protected void Btn_ThemSV_Click(object sender, EventArgs e)
    {

        //if (Btn_ThemSV.Text == "Hủy")
        //{
        //    //ClearText();
        //    Btn_ThemSV.Text = "Thêm";
        //    //DisableText();
        //    Btn_LuuSV.Enabled = false;
        //    Btn_SuaSV.Enabled = true;
        //    Btn_XoaSV.Enabled = true;
        //    GridView1.Enabled = true;
        //}
        //else
        //{
        //    //ClearText();
        //    Btn_ThemSV.Text = "Hủy";
        //    //EnableText();
        //    Btn_LuuSV.Enabled = true;
        //    Btn_SuaSV.Enabled = false;
        //    Btn_XoaSV.Enabled = false;
        //    ViewState["Them"] = true;
        //    //Them = true;
        //    //txtMSSV.Focus();
        //    //txtMSSV.Text = TuDongSinhMaHocVien();
        //    //txtLop.Focus();
        //    GridView1.Enabled = false;

        //}

        //string selectedValue = Request.Form["MyRadioButton"];
        //TextBox1.Text = selectedValue;
    }

    //Button Sửa
    protected void Btn_SuaSV_Click(object sender, EventArgs e)
    {

       /* if (txtMSSV.Text != "")
        {
            if (Btn_SuaSV.Text == "Hủy")
            {
                ClearText();
                DisableText();
                Btn_SuaSV.Text = "Sửa";
                Btn_ThemSV.Enabled = true;
                Btn_XoaSV.Enabled = true;
                Btn_LuuSV.Enabled = false;
            }
            else
            {
                ViewState["Them"] = false;
                //Them = false;
                EnableText();
                //ClearText();
                Btn_ThemSV.Enabled = false;
                Btn_XoaSV.Enabled = false;
                Btn_SuaSV.Text = "Hủy";
                Btn_LuuSV.Enabled = true;
                txtMSSV.Focus();
            }
        }
        else
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Lỗi: Chọn sinh viên để sửa!');", true);
        }*/
    }

    //Button Lưu
    protected void Btn_LuuSV_Click(object sender, EventArgs e)
    {

    /*    try
        {
            if (Them)
            {
                if (!hv.KiemTraMaHV(txtMSSV.Text))
                {
                    hv.ThemHocVien(txtMSSV.Text, txtHoTenSV.Text, txtLop.Text, ddlGioiTinh.SelectedValue, txtNgaySinh.Text, txtDiaChi.Text, txtEmail.Text, txtSoDT.Text, txtSoDT.Text);

                    ClearText();
                    DisableText();
                    Btn_ThemSV.Text = "Thêm";
                    Btn_SuaSV.Enabled = true;
                    Btn_LuuSV.Enabled = false;
                    Btn_XoaSV.Enabled = true;
                }
                else
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Lỗi: Trùng mã sinh viên!');", true);
            }
            else
            {
                hv.CapNhatHV(txtMSSV.Text, txtHoTenSV.Text, txtLop.Text, ddlGioiTinh.SelectedValue, txtNgaySinh.Text, txtDiaChi.Text, txtEmail.Text, txtSoDT.Text, txtSoDT.Text);
                ClearText();
                DisableText();
                Btn_SuaSV.Text = "Sửa";
                Btn_ThemSV.Enabled = true;
                Btn_LuuSV.Enabled = false;
                Btn_XoaSV.Enabled = true;
            }
            LoadDanhSach();
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('" + ex.Message + "');", true);
        }
        //Lỗi:Xuất hiện lỗi khi lưu dữ liệu!
        */
    }

    //Button Xóa
    protected void Btn_XoaSV_Click(object sender, EventArgs e)
    {
     /*   try
        {
            if (GridView1.SelectedIndex > 0)
            {
                hv.XoaHocVien(GridView1.SelectedRow.Cells[0].Text);
                LoadDanhSach();
                ClearText();
            }
            else
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Lỗi:Chọn sinh viên để xóa!');", true);
        }
        catch (Exception ex)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('" + ex.Message + "');", true);
        }*/
    }
    protected void btnEditPerson_Click(object sender, EventArgs e)
    {
        //ImageButton btnEdit = sender as ImageButton;
        //GridViewRow row = (GridViewRow)btnEdit.NamingContainer;

        //hidPersonEditIndex.Value = b.Scrub(row.Cells[0].Text);
        //txtMSSV.Text = b.Scrub(row.Cells[0].Text);
        //txtHoTenSV.Text = b.Scrub(row.Cells[1].Text);
        //txtLop.Text = b.Scrub(row.Cells[2].Text);
        //txtNgaySinh.Text = b.Scrub(row.Cells[4].Text);
        //txtDiaChi.Text = b.Scrub(row.Cells[5].Text);
        //txtEmail.Text = b.Scrub(row.Cells[6].Text);
        //txtSoDT.Text = b.Scrub(row.Cells[7].Text);
        ////Them = false;

        //this.UpdatePanel1.Update();
        //this.mpePerson.Show();

    }

    protected void btnSavePerson_Click(object sender, EventArgs e)
    {
        //DataClassesDataContext context = new DataClassesDataContext();
        //if (Them)
        //{
        //    tbl_SINHVIEN sv = new tbl_SINHVIEN();
        //    sv.TenSinhVien = txtHoTenSV.Text;
        //    sv.Lop = txtLop.Text;
        //    sv.GioiTinh = Convert.ToBoolean(ddlGioiTinh.SelectedValue);
        //    sv.NgaySinh = Convert.ToDateTime(txtNgaySinh.Text);
        //    sv.DiaChi = txtDiaChi.Text;
        //    sv.SoDT = txtSoDT.Text;
        //}
        ////hv.CapNhatHV(txtMSSV.Text, txtHoTenSV.Text, txtLop.Text, ddlGioiTinh.SelectedValue, txtNgaySinh.Text, txtDiaChi.Text, txtEmail.Text, txtSoDT.Text, txtSoDT.Text);
        ////DataClassesDataContext context = new DataClassesDataContext();
        ////string EID = hidPersonEditIndex.Value.ToString();
        ////tbl_SINHVIEN sv = context.tbl_SINHVIENs.Single(p => p.MsSinhVien == EID);
        ////sv.TenSinhVien = txtHoTenSV.Text;
        ////sv.Lop = txtLop.Text;
        ////sv.GioiTinh = Convert.ToBoolean(ddlGioiTinh.SelectedValue);
        ////sv.NgaySinh = Convert.ToDateTime(txtNgaySinh.Text);
        ////sv.DiaChi = txtDiaChi.Text;
        ////sv.SoDT = txtSoDT.Text;

        //context.SubmitChanges();
        //this.GridView1.DataBind();
        //this.UpdatePanel1.Update();
        //this.mpePerson.Hide();
    }
    #endregion

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView_HocVien.PageIndex = e.NewPageIndex;
        LoadDanhSach();
    }
     
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        
        if (e.CommandName == "MyDelete")
        {
            DataClassesDataContext context = new DataClassesDataContext();
            string EID = Convert.ToString(e.CommandArgument);

            context.tbl_SINHVIENs.DeleteOnSubmit(context.tbl_SINHVIENs.Single(p => p.MsSinhVien == EID));

            context.SubmitChanges();
            LoadDanhSach();
        }
    }
}
